<template>
    <div class="content-wrapper">
        <el-row class="content-row">
            <el-col class="height-init cityTree" :span="3">
                <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
            </el-col>
            <el-col class="height-init" :span="21">
                <div class="container">
                    <el-row class="handle-box">
                        <el-select v-model="query.spaceId" class="mr10" filterable clearable placeholder='请选择广告位置' >
                            <el-option
                                    v-for="item in spaces"
                                    :key="item.spaceId"
                                    :label="item.name"
                                    :value="item.spaceId">
                            </el-option>
                        </el-select>
                        <el-select v-model="query.status" class="mr10" filterable clearable placeholder='请选择状态' >
                            <el-option
                                    v-for="item in statusOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                        <el-button type="primary" icon="el-icon-plus" v-if="checkAuth(auths.add)" class="handle-del" @click="addAdvertise">新增</el-button>
                    </el-row>
                    <el-row class="list-table-row">
                        <el-table
                                :data="tableData"
                                border
                                class="table"
                                ref="multipleTable"
                                height="100%"
                                header-cell-class-name="table-header"
                                @selection-change="handleSelectionChange"
                        >
                            <el-table-column type="selection" width="55" align="center"></el-table-column>
                            <el-table-column type="index" width="50" align="center"></el-table-column>
                            <el-table-column prop="title" label="广告主题" show-overflow-tooltip />
                            <el-table-column prop="name" label="投放板块" show-overflow-tooltip>
                                <template slot-scope="scope">
                                    {{scope.row.page}}/{{scope.row.name}}
                                </template>
                            </el-table-column>
                            <el-table-column prop="cityName" label="投放地区" show-overflow-tooltip>
                                <template slot-scope="scope">
                                    <span v-if="scope.row.scope===1">全国</span>
                                    <span v-if="scope.row.scope===2">{{scope.row.cityName}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="money" label="上架时间" show-overflow-tooltip>
                                <template slot-scope="scope" >
                                    <span>{{renderTimeWithOutYear(scope.row.startTime)}}~{{renderTimeWithOutYear(scope.row.endTime)}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="当前状态" align="center">
                                <template slot-scope="scope" >
                                    <span v-if="scope.row.status == 0">草稿</span>
                                    <span v-if="scope.row.status == 2">已下架</span>
                                    <span v-if="scope.row.status == 1">
                                        <span v-if="scope.row.startTime<=new Date().getTime() && scope.row.endTime>=new Date().getTime()">上架中</span>
                                        <span v-if="scope.row.startTime>new Date().getTime()">等待上架</span>
                                        <span v-if="scope.row.endTime<new Date().getTime()">已结束</span>
                                      </span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip>
                                <template slot-scope="scope" >
                                    {{renderTime(scope.row.createTime)}}
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" width="160" align="center">
                                <template slot-scope="scope">
                                    <el-button type="text" size="small" @click="viewDetail(scope.row)" >详情</el-button>
                                    <el-button v-if="checkAuth(auths.update) && scope.row.status != 2" type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)" >编辑</el-button>
                                    <el-button type="text" v-if="checkAuth(auths.delete) && scope.row.status===0" icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row)" >删除</el-button>
                                    <el-button type="text" v-if="checkAuth(auths.down) && scope.row.status===1 && scope.row.startTime<=new Date().getTime() && scope.row.endTime>new Date().getTime()" size="small" @click="handleDown(scope.$index, scope.row)" >下架</el-button>
                                    <el-button type="text" v-if="checkAuth(auths.up) && scope.row.status===2 " size="small" @click="handleUp(scope.$index, scope.row)" >上架</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-row>
                    <el-row class="pagination-self">
                        <el-pagination
                                background
                                layout="total, sizes, prev, pager, next"
                                :current-page="query.pageNo"
                                :page-size="query.pageSize"
                                :total="pageTotal"
                                @size-change="handleSizeChange"
                                @current-change="handlePageChange"
                        ></el-pagination>
                    </el-row>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import { fetchList, updateAdvertise } from '../../../../api/advertise/advertise'
import { getMoreSpace } from '../../../../api/advertise/advertiseSpace'
import { getCityTree } from '../../../../api/city'

export default {
    name: 'carouselList',
    data() {
        return {
            query: {
                spaceId:'',
                status:'',
                pageNo: 1,
                pageSize: 20,
                cityCode:'',
                type:1,
                category:2
            },
            tableData: [],
            multipleSelection: [],
            pageTotal: 0,
            spaces:[],
            statusOptions:[{
                value:'0',
                label:'草稿'
            },{
                value:'2',
                label:'已下架'
            },{
                value:'3',
                label:'上架中'
            },{
                value:'4',
                label:'等待上架'
            },{
                value:'5',
                label:'已结束'
            }],
            data: [],
            defaultProps: {
                children: 'children',
                label: 'name',
                isLeaf: 'leaf'
            }
        };
    },
    activated(){
        this.getData();
        this.getMoreSpace();
        this.getCityTree();
    },
    created() {
        this.getData();
        this.getMoreSpace();
        this.getCityTree();
    },
    props:['auths'],
    methods: {
        //广告详情
        viewDetail(row){
            this.addTag('轮播广告详情','/index/advertise/carousel/detail/'+row.id,'carouselDetail');
        },
        /**
         * 获取城市树
         */
        getCityTree(){
            getCityTree().then(res=>{
                this.data = res.data;
            })
        },
        /**
         * 获取坑位
         */
        getMoreSpace(){
            getMoreSpace({type:1,category:2}).then(res=>{
                this.spaces = res.data;
            })
        },
        /**
         * 点击树
         */
        handleNodeClick(data) {
            this.query.cityCode = data.code;
            this.getData();
        },
        /**
         * 获取列表
         */
        getData() {
            fetchList(this.query).then(res => {
                this.tableData = res.data.records;
                this.pageTotal = res.data.total || 50;
            });
        },
        /**
         * 新增广告
         */
        addAdvertise(){
            this.addTag('轮播广告添加','/index/advertise/carousel/add','carouselAdd')
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageNo', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                updateAdvertise({id:row.id,operation:'deleted'}).then(res => {
                    if (res.code == '0') {
                        this.$message.success('删除成功');
                        this.getData();
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                });
            }).catch(() => {});
        },
        //下架操作
        handleDown(index, row){
            // 二次确认下架
            this.$confirm('确定要下架吗？', '提示', {
                type: 'warning'
            }).then(() => {
                updateAdvertise({id:row.id,operation:'obtained'}).then(res => {
                    if (res.code == '0') {
                        this.$message.success('下架成功');
                        this.getData();
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                });
            }).catch(() => {});
        },
        //上架
        handleUp(index, row){
            // 二次确认上架
            this.$confirm('确定要上架吗？', '提示', {
                type: 'warning'
            }).then(() => {
                updateAdvertise({id:row.id,operation:'up'}).then(res => {
                    if (res.code == '0') {
                        this.$message.success('上架成功');
                        this.getData();
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                });
            }).catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 编辑操作
        handleEdit(index, row) {
            this.addTag('轮播广告编辑','/index/advertise/carousel/edit/'+row.id,'carouselEdit');
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageNo', val);
            this.getData();
        },
        handleSizeChange(val) {
            this.$set(this.query, 'pageSize', val);
            this.getData();
        },
    }
};
</script>

<style scoped>

.content-wrapper{
    height:100%;
    overflow-y: hidden;
}

.content-row{
    height: 100%;
}

.cityTree{
    overflow-y: scroll;
}

.height-init{
    height: 100%;
}

.container{
    height: 100%;
}

.handle-box {
    height: 6.5%;
}

.handle-input {
    width: 120px;
    display: inline-block;
}

.list-table-row{
    height:83%;
}

.table {
    width: 100%;
    height: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.pagination-self{
    height:10%;
    text-align: right;
    margin-top: 5px;
}
</style>
